<template>
  <div id="main">
    <h2>教学总结管理</h2>
    <div class="head">
      <el-button v-if="this.jurisdiction ==='授课老师'" type="primary" style="margin:20px" @click="handleOpen('添加员工', 1)">添加教学总结</el-button>
      <el-input
        v-model="searchTableInfo"
        placeholder="请输入搜索内容"
        style="width:240px;float: right;margin:20px;"
      />

      <div id="table-area">
        <el-table
          v-loading="listLoading"
          :data="
            list.slice((currentPage - 1) * pagesize, currentPage * pagesize)
          "
          border
          fit
          highlight-current-row
        >
          <el-table-column label="序号" type="index" align="center">
            <template scope="scope">
              <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            prop="studentName"
            label="学生姓名"
          />
          <el-table-column
            align="center"
            prop="teachStuid"
            label="学生学号"
          />
          <el-table-column
            align="center"
            prop="studentAge"
            label="年龄"
          />
          <el-table-column
            align="center"
            prop="studentSex"
            label="学生性别"
          />
          <el-table-column
            align="center"
            prop="studentJoinTime"
            label="训练时长"
            class-name="天"
          />

          <el-table-column
            align="center"
            prop="clazzType"
            label="课程类型"
          />
          <el-table-column
            align="center"
            prop="technicalTypeName"
            label="技术类型"
          />
          <el-table-column
            align="center"
            prop="teacherName"
            label="授课教师"
          />
          <el-table-column
            align="center"
            prop="responsibleTeacherName"
            label="主责教师"
          />

          <el-table-column
            align="center"
            prop="teachCreatetime"
            :formatter="dateFormat"
            label="录入时间"
          />

          <el-table-column align="center" label="操作">
            <template slot-scope="Scope">
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleOpen('修改总结', 2, Scope.row.teachStuid)"
              >查看</el-button>

              <el-button

                type="danger"
                icon="el-icon-delete"
                @click="handleDelete( Scope.row.teachStuid)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div id="pages">
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[5, 10]"
            :page-size="pagesize"
            layout="total, sizes,prev, pager, next"
            :total="list.length"
            prev-text="上一页"
            next-text="下一页"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <el-form
        ref="summary"
        :model="summary"
        label-position="top"
        :rules="rules"
      >
        <el-form-item prop="teachPostUserId" label="授课老师" :label-width="formLabelWidth">
          <!-- <el-input v-model="teachTodo.teachingTodoRelatedPersonnel" autocomplete="off" maxlength="20" show-word-limit /> -->
          <el-select v-model="summary.teachPostUserId" placeholder="请选择" filterable style="width: 100%;">
            <el-option
              v-for="j in staffTeacher"
              :key="j.staffId"
              :value="j.staffId"
              :label="j.String"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="teachStuid" label="学生" :label-width="formLabelWidth">
          <!-- <el-input v-model="teachTodo.teachingTodoRelatedPersonnel" autocomplete="off" maxlength="20" show-word-limit /> -->
          <el-select v-model="summary.teachStuid" placeholder="请选择" filterable style="width: 100%;">
            <el-option
              v-for="j in child"
              :key="j.studentId"
              :value="j.studentId"
              :label="j.String"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="teachClazzId" label="班级" :label-width="formLabelWidth">
          <!-- <el-input v-model="teachTodo.teachingTodoRelatedPersonnel" autocomplete="off" maxlength="20" show-word-limit /> -->
          <el-select v-model="summary.teachClazzId" placeholder="请选择" filterable style="width: 100%;">
            <el-option
              v-for="j in clazz"
              :key="j.classId"
              :value="j.classId"
              :label="j.className"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="teachEvaluation" label="教学评价" :label-width="formLabelWidth">
          <el-input
            v-model="summary.teachEvaluation"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
            @input="change($event)"
          />
        </el-form-item>

        <el-form-item prop="teachStudentEvaluation" label="学生评价" :label-width="formLabelWidth">
          <el-input
            v-model="summary.teachStudentEvaluation"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
            @input="change($event)"
          />
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button

          v-if="(editType == 2 && this.jurisdiction ==='授课老师') || this.jurisdiction ==='责任教师'|| this.jurisdiction ==='教学主管'"
          type="primary"
          @click="handleUpdate('summary')"
        >修 改</el-button>
        <el-button
          v-if="editType == 1 && this.jurisdiction ==='授课老师'"
          type="primary"
          @click="handleAddUser('summary')"
        >添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { validateChineseName, validatePhoneTwo, validateEMail, checkSpecificKey, validateCharacter, btKeyUp, isInteger } from '@/api/validata'
import { selectStudent } from '@/api/child'
import { selectstaffTeacher } from '@/api/staff'
import store from '@/store/index'
import {
  updateteachsummary,
  deleteteachsummary,
  getAllteachsummary,
  getteachsummarybyId,
  addoneteachsummary,
  getAllteachsummaryByMedical,
  getAllteachsummaryByDirector,
  getAllteachsummaryByprincipal,
  getAllteachsummaryAA,
  selectclazz
  // getAllstudentName
} from '@/api/teachsummary'
import moment from 'moment'
export default {
  data() {
    return {
      jurisdiction: '',
      BB: store.getters.userId,
      value: '',
      clazz: [],
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      tableList: [],
      searchTableInfo: '',
      summary: {},
      child: {},
      listLoading: false,
      dialogFormVisible: false,
      editType: 1, // 1 添加 2 修改
      student: {},
      search: '',
      staffTeacher: [],
      teachCreatetime: '',
      formLabelWidth: '200px',
      rules: {
        teachDifficulties: [
          { required: true, message: '教学难点存在的问题不能为空', trigger: 'blur' },
          { validator: validateChineseName, message: '请输入中文', trigger: ['blur', 'change'] }
        ],
        teachMedicalAdvice: [
          { required: true, message: '医学建议不能为空', trigger: 'blur' },
          { validator: validateChineseName, message: '请输入中文', trigger: ['blur', 'change'] }
        ],
        teachStageSum: [
          { required: true, message: '阶段总结内容不能为空', trigger: 'blur' },
          { validator: validateChineseName, message: '请输入中文', trigger: ['blur', 'change'] }
        ],
        teachSuggestions: [
          { required: true, message: '教学建议不能为空', trigger: 'blur' },
          { validator: validateChineseName, message: '请输入中文', trigger: ['blur', 'change'] }
        ],
        teachSum: [
          { required: true, message: '教学总结内容不能为空', trigger: 'blur' },
          { validator: validateChineseName, message: '请输入中文', trigger: ['blur', 'change'] }
        ],
        teachCreatetime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        teachStuid: [
          { required: true, message: '学生姓名不能为空', trigger: 'blur' }
        ]
      },
      newsummary: {
        teachCreatetime: ''
        // teachId: ""
      }
    }
  },
  computed: {
    // 根据计算属性模糊搜索
    list() {
      const searchTableInfo = this.searchTableInfo

      if (searchTableInfo) {
        // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
        // 注意： filter() 不会对空数组进行检测。
        // 注意： filter() 不会改变原始数组。
        return this.tableList.filter(data => {
          // some() 方法用于检测数组中的元素是否满足指定条件;
          // some() 方法会依次执行数组的每个元素：
          // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
          // 如果没有满足条件的元素，则返回false。
          // 注意： some() 不会对空数组进行检测。
          // 注意： some() 不会改变原始数组。
          return Object.keys(data).some(key => {
            // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
            // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
            return (
              String(data[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            )
          })
        })
      }
      return this.tableList
    }
  },
  created() {
    this.jurisdiction = store.getters.roles[0]

    this.fetchData()
    // this.getAllstudent();
    this.getinfo()
  },
  methods: {
    // 时间格式化
    dateFormat(row, column) {
      var date = row[column.property]
      if (date == undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    // 时间格式化
    change(e) {
      this.$forceUpdate()
    },
    // formatSex: function(row, column, cellValue) {
    //   if (cellValue === '1') {
    //     return '女'
    //   } else if (cellValue === '0') {
    //     return '男'
    //   }
    // },

    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    // getAllstudent() {
    //   getAllstudentName().then(res => {
    //     this.student = res.obj;
    //   });
    // },
    getinfo: function() {
      selectStudent().then((resp) => {
        this.child = resp.obj
      })
      selectstaffTeacher().then((resp) => {
        this.staffTeacher = resp.obj
      })
      selectclazz().then((resp) => {
        this.clazz = resp.obj
      })
    },
    handleClick(row) {

    },
    fetchData() {
      this.tableList = []
      this.listLoading = true
      if (this.jurisdiction === '医疗主管') {
        getAllteachsummaryByMedical(this.BB).then(data => {
          this.newsummary
          for (var i = 0; i < data.obj.length; i++) {
            this.newsummary.clazzType = data.obj[i].clazzType
            this.newsummary.responsibleTeacherName = data.obj[i].responsibleTeacherName
            this.newsummary.studentAge = data.obj[i].studentAge
            this.newsummary.studentJoinTime = data.obj[i].studentJoinTime
            this.newsummary.studentName = data.obj[i].studentName
            this.newsummary.studentSex = data.obj[i].studentSex
            this.newsummary.technicalTypeName = data.obj[i].technicalTypeName
            this.newsummary.teacherName = data.obj[i].teacherName
            this.newsummary.teachCreatetime = data.obj[i].teachCreatetime
            this.newsummary.teachStuid = data.obj[i].teachStuid
            this.newsummary.teachId = data.obj[i].teachId

            this.tableList.push(this.newsummary)
            this.newsummary = {}
          }

          this.listLoading = false
        })
      } else if (this.jurisdiction === '教学主任') {
        getAllteachsummaryAA(this.BB).then(data => {
          this.newsummary
          for (var i = 0; i < data.obj.length; i++) {
            this.newsummary.clazzType = data.obj[i].clazzType
            this.newsummary.responsibleTeacherName = data.obj[i].responsibleTeacherName
            this.newsummary.studentAge = data.obj[i].studentAge
            this.newsummary.studentJoinTime = data.obj[i].studentJoinTime
            this.newsummary.studentName = data.obj[i].studentName
            this.newsummary.studentSex = data.obj[i].studentSex
            this.newsummary.technicalTypeName = data.obj[i].technicalTypeName
            this.newsummary.teacherName = data.obj[i].teacherName
            this.newsummary.teachCreatetime = data.obj[i].teachCreatetime
            this.newsummary.teachStuid = data.obj[i].teachStuid
            this.newsummary.teachId = data.obj[i].teachId

            this.tableList.push(this.newsummary)
            this.newsummary = {}
          }

          this.listLoading = false
        })
      } else if (this.jurisdiction === '责任教师') {
        getAllteachsummaryByprincipal(this.BB).then(data => {
          this.newsummary
          for (var i = 0; i < data.obj.length; i++) {
            this.newsummary.clazzType = data.obj[i].clazzType
            this.newsummary.responsibleTeacherName = data.obj[i].responsibleTeacherName
            this.newsummary.studentAge = data.obj[i].studentAge
            this.newsummary.studentJoinTime = data.obj[i].studentJoinTime
            this.newsummary.studentName = data.obj[i].studentName
            this.newsummary.studentSex = data.obj[i].studentSex
            this.newsummary.technicalTypeName = data.obj[i].technicalTypeName
            this.newsummary.teacherName = data.obj[i].teacherName
            this.newsummary.teachCreatetime = data.obj[i].teachCreatetime
            this.newsummary.teachStuid = data.obj[i].teachStuid
            this.newsummary.teachId = data.obj[i].teachId

            this.tableList.push(this.newsummary)
            this.newsummary = {}
          }

          this.listLoading = false
        })
      } else if (this.jurisdiction === '教学主管') {
        getAllteachsummaryByDirector(this.BB).then(data => {
          this.newsummary
          for (var i = 0; i < data.obj.length; i++) {
            this.newsummary.clazzType = data.obj[i].clazzType
            this.newsummary.responsibleTeacherName = data.obj[i].responsibleTeacherName
            this.newsummary.studentAge = data.obj[i].studentAge
            this.newsummary.studentJoinTime = data.obj[i].studentJoinTime
            this.newsummary.studentName = data.obj[i].studentName
            this.newsummary.studentSex = data.obj[i].studentSex
            this.newsummary.technicalTypeName = data.obj[i].technicalTypeName
            this.newsummary.teacherName = data.obj[i].teacherName
            this.newsummary.teachCreatetime = data.obj[i].teachCreatetime
            this.newsummary.teachStuid = data.obj[i].teachStuid
            this.newsummary.teachId = data.obj[i].teachId

            this.tableList.push(this.newsummary)
            this.newsummary = {}
          }

          this.listLoading = false
        })
      } else {
        getAllteachsummary(this.BB).then(data => {
          this.newsummary
          for (var i = 0; i < data.obj.length; i++) {
            this.newsummary.clazzType = data.obj[i].clazzType
            this.newsummary.responsibleTeacherName = data.obj[i].responsibleTeacherName
            this.newsummary.studentAge = data.obj[i].studentAge
            this.newsummary.studentJoinTime = data.obj[i].studentJoinTime
            this.newsummary.studentName = data.obj[i].studentName
            this.newsummary.studentSex = data.obj[i].studentSex
            this.newsummary.technicalTypeName = data.obj[i].technicalTypeName
            this.newsummary.teacherName = data.obj[i].teacherName
            this.newsummary.teachCreatetime = data.obj[i].teachCreatetime
            this.newsummary.teachStuid = data.obj[i].teachStuid
            this.newsummary.teachId = data.obj[i].teachId

            this.tableList.push(this.newsummary)
            this.newsummary = {}
          }

          this.listLoading = false
        })
      }
    },
    handleDelete(id, username) {
      this.$confirm('确认删除该项总结？').then(() =>
        deleteteachsummary(id).then(data => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil(
            (this.tableList.length - 1) / this.pagesize
          )
          const page =
            this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      )
    },
    handleAddUser(formName) {
      // this.summary.teachCreatetime = this.summary.teachCreatetime

      this.$refs[formName].validate(valid => {
        if (valid) {
          addoneteachsummary(this.summary).then(resp => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.summary = {}
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleOpen(title, type, id) {
      this.summary = {}
      this.dialogFormVisible = true
      this.title = title
      this.editType = type
      if (type === 2) {
        getteachsummarybyId(id).then(resp => {
          this.summary = resp.obj
          this.summary.teachEvaluation = resp.obj[0].teachEvaluation
          this.summary.teachStudentEvaluation = resp.obj[0].teachStudentEvaluation
          this.summary.teachId = resp.obj[0].teachId
          this.summary.teachClazzId = resp.obj[0].teachClazzId
          this.summary.teachStuid = resp.obj[0].teachStuid
          this.summary.teachPostUserId = resp.obj[0].teachPostUserId
        })
      }
    },
    handleClose() {
      this.dialogFormVisible = false
      this.summary = {}
    },

    handleUpdate(formName) {
      // this.summary.teachCreatetime = this.summary.teachCreatetime

      this.$refs[formName].validate(valid => {
        if (valid) {
          updateteachsummary(this.summary).then(resp => {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
</style>
